<?php
namespace Models\Model;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
class UserPostsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addpost($data)
    {
		$pddate			=	$data['proposeddate'];
		$date 			= 	date_create($pddate);
		$proposedDate	=	date_format($date, 'Y-m-d H:i:s A');
		$udate			=	$data['untildate'];
		$date 			= 	date_create($udate);
		$untilDate		=	date_format($date, 'Y-m-d H:i:s A');
		$data = array(
			'user_id' 	  	      	    => $data['user_id'],		
			'post_message' 		        => $data['message'],  		
			'venue' 		            => $data['venue'],  		
			'proposed_date_time' 	    => $proposedDate,		
			'until_date_time' 		    => $untilDate,
			'post_setting_rating' 		=> $data['settingrating'],			
			'post_radius' 		        => $data['radius'],			
			'post_pincode' 		        => $data['pincode'],			
			'added_date' 		        => date('Y-m-d H:i:s'),			
			'updated_time' 		        => date('Y-m-d H:i:s'),			
			'status' 		            => 0	 				
		);
		$insertresult=$this->tableGateway->insert($data);
		return $this->tableGateway->lastInsertValue;
		
    }
	
	public function checkPoststoday($userid){  
		$select = $this->tableGateway->getSql()->select();
		$select->join('users', new Expression('users.user_id=user_posts.user_id'),array('*'),'left');
	    $select->where('user_posts.user_id="'.$userid.'"');                                         
	    //$select->where('DATE(user_posts.proposed_date_time)="'.date('Y-m-d').'"');
		$select->order('user_posts.user_post_id DESC');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	
	public function serchPoststoday($data){  
		$select = $this->tableGateway->getSql()->select();
		$select->join('users', new Expression('users.user_id=user_posts.user_id'),array('*'),'left');
	    $select->where('user_posts.user_id="'.$data['user_id'].'"');                                         
	    //$select->where('DATE(user_posts.proposed_date_time)="'.date('Y-m-d').'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function getMessage($user_post_id){  
		$select = $this->tableGateway->getSql()->select();
	    $select->where('user_post_id="'.$user_post_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function getListsByRating($rating,$userID,$type,$pincode)
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->join('users', new Expression('users.user_id=user_posts.user_id'),array('partner_lat'=>'lat','partner_long'=>'long','partner_id'=>'user_id','p_DeviceId'=>'device_id','p_platform'=>'platform'),'left');
		$select->join(array('u' => 'users'), new Expression('u.user_id='.$userID),array('lat','long'),'left');
	    $select->join('user_settings', new Expression('user_settings.user_id=users.user_id'),array('rating','radius','notifications_status'),'left');
		$select->where('user_posts.post_pincode="'.$pincode.'"');
		$select->where('user_posts.post_setting_rating="'.$rating.'"');
		if($type==1){
			$select->where('user_settings.notifications_status="1"');
			//$select->where('user_posts.post_rating="'.$rating.'"');
		}
		$select->where('user_posts.user_id!="'.$userID.'"');
		$select->group('user_posts.user_id');
		$resultSet = $this->tableGateway->selectWith($select);
		//echo "<pre>";print_r($resultSet);exit;
		return $resultSet;	
    }
	
	
	public function getliststoday($rating,$userID,$userIDS,$radius)
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_posts_alerts', new Expression('user_posts_alerts.user_post_id=user_posts.user_post_id  AND user_posts_alerts.partner_id='.$userID),array('upastatus'=>'status','confirm_status','user_post_replies_id'),'left');
		$select->join('users', new Expression('users.user_id=user_posts.user_id'),array('user_name','phone','address','device_id','platform','pincode'),'left');
		$select->where(array('user_posts.user_id'=>$userIDS));
		//$select->where('user_posts.post_rating="'.$rating.'"');
		$select->where('user_posts.post_setting_rating="'.$rating.'"');
		$select->order('user_posts.user_post_id DESC');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;		
    }
	public function searchliststoday($rating,$userData)
    {	
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_posts_alerts', new Expression('user_posts_alerts.user_post_id=user_posts.user_post_id  AND user_posts_alerts.partner_id='.$userData['user_id']),array('upastatus'=>'status','confirm_status','user_post_replies_id'),'left');
		$select->join('users', new Expression('users.user_id=user_posts.user_id'),array('user_name','phone','address','pincode'),'left');
		$select->where('user_posts.post_rating="'.$rating.'"');
		$select->where('user_posts.user_id !="'.$userData['user_id'].'"');
		$search[]=new Predicate\Like('users.user_name', '%'. $userData['search'] .'%');
		$search[]=new Predicate\Like('users.address', '%'. $userData['search'] .'%');
		$search[]=new Predicate\Like('users.phone', '%'. $userData['search'] .'%');
		$search[]=new Predicate\Like('users.pincode', '%'. $userData['search'] .'%');
		$select->where(array(
						new Predicate\PredicateSet(
							$search,
							Predicate\PredicateSet::COMBINED_BY_OR
						),
					));
		//$select->where->like( 'users.user_name', '%'. $userData['search'] .'%' );
		//$select->where->like( 'users.address', '%'. $userData['search'] .'%' );
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;		
    }
}